import PropTypes from 'prop-types'
import React, { Component } from 'react'
import { Menu } from 'antd'
import { Link, Redirect, Route, Switch as SwitchRouter } from 'react-router-dom'
import styles from './AdminPage.module.css'
import { connect } from 'react-redux'
import AdminUserPage from './AdminUserPage'
import AdminDepartmentPage from './AdminDepartmentPage'
import AdminSalaryPage from './AdminSalaryPage'
import { admin_load_access_token, admin_logout } from '../../../_redux/actions'
import AdminAllDepartmentPage from './AdminAllDepartmentPage'

class AdminMenu extends Component {
  static propTypes = {
    handleLogout: PropTypes.func
  }

  constructor (props) {
    super(props)
  }

  render () {

    return (
      <div className={styles.menu}>
        <Menu
          mode="inline"
          theme="dark"
          className={styles.menuContainer}
        >
          <Menu.Item key="1">
            <Link target="_blank" to="/admin/department/1">部门</Link>
          </Menu.Item>
          <Menu.Item key="2">
            <Link target="_blank" to="/admin/users">用户</Link>
          </Menu.Item>
          <Menu.Item key="3">
            <a onClick={this.props.handleLogout}>登出</a>
          </Menu.Item>
        </Menu>
      </div>
    )
  }
}

export default class AdminPage extends Component {
  constructor (props) {
    super(props)
    this.state = {
      loginFlag: false
    }
  }

  handleLogout = () => {
    this.props.admin_logout()
  }

  render () {
    if (!this.props.accessToken) {
      return <Redirect to="/login"/>
    }

    return (
      <div className={styles.container}>
        <AdminMenu handleLogout={this.handleLogout}/>
        <SwitchRouter>
          <Route path="/admin/users" component={AdminUserPage}/>
          <Route path="/admin/department/all" component={AdminAllDepartmentPage} />
          <Route path="/admin/department/:departmentId" component={AdminDepartmentPage}/>
          <Route path="/admin/salary/:userId" component={AdminSalaryPage}/>
          <Redirect to="/admin/users"/>
        </SwitchRouter>
      </div>
    )
  }
}

AdminPage = connect(
  ({ $AdminPageState: { accessToken } }) => ({ accessToken }),
  { admin_logout, admin_load_access_token }
)(AdminPage)